package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgShopSectionMapper;
import com.ztgf.shopping.models.bo.epg.*;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgShopSectionDTO;
import com.ztgf.shopping.models.dto.template.TempletModuleDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgShopSectionService;
import com.ztgf.shopping.models.vo.epg.EpgShopSectionCommodityRelationVO;
import com.ztgf.shopping.models.vo.epg.EpgShopSectionVO;
import com.ztgf.shopping.service.template.TempletInfoServiceImpl;
import com.ztgf.shopping.service.template.TempletModuleServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 店铺频道栏目控制器
 *
 * @author YUE
 * @Date 2020-05-28 18:39:56
 */
@DubboService
public class EpgShopSectionServiceImpl extends ServiceImpl<EpgShopSectionMapper, EpgShopSectionDTO> implements IEpgShopSectionService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgShopSectionMapper epgShopSectionMapper;

    @Autowired
    private TempletInfoServiceImpl templetInfoService;

    @Autowired
    private TempletModuleServiceImpl templetModuleService;

    @Autowired
    private EpgShopSectionCommodityRelationServiceImpl epgShopSectionCommodityRelationService;

    @Override
    public boolean add(EpgShopSectionSaveBO saveBO) {
        EpgShopSectionDTO dto = new EpgShopSectionDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    public Long addAndReturnId(EpgShopSectionSaveBO saveBO) {
        EpgShopSectionDTO dto = new EpgShopSectionDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        this.insert(dto);
        return id;
    }

    /**
     * 创建默认的店铺频道
     * @param templateId
     * @param moduleId
     */
    public void createDefaultShopHomeSection(Long templateId,Long moduleId,Long shopId){
        //新品推荐
        EpgShopSectionSaveBO epgShopSectionSaveBO = new EpgShopSectionSaveBO();
        epgShopSectionSaveBO.setKey(ShoppingCodeEnum.NEW_SHOP_1.getMsg());
        epgShopSectionSaveBO.setShopId(shopId);
        epgShopSectionSaveBO.setTempletId(templateId);
        epgShopSectionSaveBO.setModuleId(moduleId);
        epgShopSectionSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgShopSectionSaveBO.setSort(0);
        epgShopSectionSaveBO.setName("新品推荐");
        add(epgShopSectionSaveBO);
        //镇店之宝
        epgShopSectionSaveBO.setName("镇店之宝");
        epgShopSectionSaveBO.setKey(ShoppingCodeEnum.SHOP_BABY_2.getMsg());
        epgShopSectionSaveBO.setSort(1);
        add(epgShopSectionSaveBO);
        //掌柜推荐
        epgShopSectionSaveBO.setName("掌柜推荐");
        epgShopSectionSaveBO.setKey(ShoppingCodeEnum.SHOPPER_RECOMMEND_3.getMsg());
        epgShopSectionSaveBO.setSort(2);
        add(epgShopSectionSaveBO);
    }

    /**
     * 根据频道添加不同的商品
     * @param ids
     * @param shopId
     * @param sectionType
     * @return
     */
    //@Async
    @Override
    public void addShopSectionBySectionType(List<Long> ids,Long shopId,String sectionType){
        EpgShopSectionVO epgShopSectionVO = getSectionBySectionType(shopId, sectionType);
        EpgShopSectionCommodityRelationSaveBO epgShopSectionCommodityRelationSaveBO = new EpgShopSectionCommodityRelationSaveBO();
        int shopNum = epgShopSectionVO.getEpgShopSectionCommodityRelationVOList().size();//商品数量
        for (Long commodityId : ids) {
            epgShopSectionCommodityRelationSaveBO.setCommodityId(commodityId);
            epgShopSectionCommodityRelationSaveBO.setSectionId(epgShopSectionVO.getId());
            if(shopNum >= 8){//已经发布出来的已经大于8个，改为未发布
                continue;//不加入进去(暂时)
                //epgShopSectionCommodityRelationSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_0.getCode());
            }else{
                epgShopSectionCommodityRelationSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                epgShopSectionCommodityRelationSaveBO.setSort(shopNum);//发布商品排序
                shopNum++;
            }
            epgShopSectionCommodityRelationService.add(epgShopSectionCommodityRelationSaveBO);
        }
    }

    /**
     * 根据频道下架不同的商品
     * @param ids
     * @param shopId
     * @param sectionType
     * @return
     */
    //@Async
    @Override
    public void deleteShopSectionBySectionType(List<Long> ids,Long shopId,String sectionType){
        EpgShopSectionDTO epgShopSectionDTO = getBySectionTypeAndShopId(shopId, sectionType);
        for (Long id : ids) {
            epgShopSectionCommodityRelationService.deleteByCommodityId(epgShopSectionDTO.getId(),id);
        }
    }


    /**
     * 根据店铺id和频道类型查询频道
     * @param shopId
     * @param sectionType
     * @return
     */
    private EpgShopSectionDTO getBySectionTypeAndShopId(Long shopId,String sectionType){
        Wrapper<EpgShopSectionDTO> wrapper = new EntityWrapper();
        wrapper.eq("shop_id", shopId);
        wrapper.eq("key1", sectionType);
        return selectOne(wrapper);
    }

    /**
     * 单独查询对应的频道根据具体的发布出来频道信息
     * @param shopId
     * @return
     */
    @Override
    public EpgShopSectionVO getSectionBySectionType(Long shopId,String sectionType){
        EpgShopSectionVO epgShopSectionVO = new EpgShopSectionVO();
        //获取默认模板
        Long templateId = templetInfoService.getDefaultTemIdByShopId(shopId);
        if(0 == templateId){
            return null;
        }
        TempletModuleDTO templetModuleDTO = templetModuleService.getSectionByTemId(templateId);
        if(null == templetModuleDTO){
            return epgShopSectionVO;
        }
        EpgShopSectionDTO epgShopSectionDTO = new EpgShopSectionDTO();
        epgShopSectionDTO.setKey(sectionType);
        epgShopSectionDTO.setShopId(shopId);
        epgShopSectionDTO.setTempletId(templateId);
        epgShopSectionDTO.setModuleId(templetModuleDTO.getId());
        epgShopSectionDTO = epgShopSectionMapper.selectOne(epgShopSectionDTO);//查询对应的频道信息
        if(null == epgShopSectionDTO){
            return null;
        }
        BeanUtils.copyProperties(epgShopSectionDTO,epgShopSectionVO);
        EpgShopSectionCommodityRelationBO epgShopSectionCommodityRelationBO = new EpgShopSectionCommodityRelationBO();
        epgShopSectionCommodityRelationBO.setSectionId(epgShopSectionVO.getId());
        epgShopSectionCommodityRelationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgShopSectionCommodityRelationBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        List<EpgShopSectionCommodityRelationVO> epgShopSectionCommodityRelationVOList = epgShopSectionCommodityRelationService.getBySelectShopSectionCommodityRelationInfo(epgShopSectionCommodityRelationBO);
        epgShopSectionVO.setEpgShopSectionCommodityRelationVOList(epgShopSectionCommodityRelationVOList);
        return epgShopSectionVO;
    }

    /**
     * 删除功能相关的店铺频道栏目
     * @return
     */
    public boolean deleteByModuleId(Long moduleId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id",moduleId);
        return epgShopSectionMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 根据条件查询 频道信息 有页码
     * @param epgShopSectionBO
     * @return
     */
    @Override
    public Page<EpgShopSectionDTO> getBySelectShopSectionInfoPage(EpgShopSectionBO epgShopSectionBO){
        epgShopSectionBO = addMustData(epgShopSectionBO);
        Wrapper<EpgShopSectionDTO> wrapper = getBySelectShopSectionInfo(epgShopSectionBO);
        return selectPage(new Page<>(epgShopSectionBO.getPageNo(),epgShopSectionBO.getPageSize()),wrapper);
    }

    /**
     * 根据条件查询 频道信息 无页码
     * @param epgShopSectionBO
     * @return
     */
    @Override
    public List<EpgShopSectionDTO> getBySelectShopSectionInfoNoPage(EpgShopSectionBO epgShopSectionBO){
        epgShopSectionBO = addMustData(epgShopSectionBO);
        Wrapper<EpgShopSectionDTO> wrapper = getBySelectShopSectionInfo(epgShopSectionBO);
        if(epgShopSectionBO.getPageNo() == 0 && epgShopSectionBO.getPageSize() == 0)
            return epgShopSectionMapper.selectPage( new Page<>(0, ShoppingConstant.SHOP_SECTION),wrapper);
        return selectList(wrapper);
    }

    /**
     * 添加必要数据
     * @param epgShopSectionBO
     * @return
     */
    private EpgShopSectionBO addMustData(EpgShopSectionBO epgShopSectionBO){
        epgShopSectionBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgShopSectionBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgShopSectionBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgShopSectionBO;
    }

    private Wrapper<EpgShopSectionDTO> getBySelectShopSectionInfo(EpgShopSectionBO epgShopSectionBO){
        Wrapper<EpgShopSectionDTO> wrapper = new EntityWrapper<>();
        EpgShopSectionDTO epgShopSectionDTO = new EpgShopSectionDTO();
        BeanUtils.copyProperties(epgShopSectionBO,epgShopSectionDTO);
        wrapper.eq("status", epgShopSectionBO.getStatus());
        wrapper.eq("is_publish", epgShopSectionBO.getIsPublish());
        wrapper.eq("module_id", epgShopSectionBO.getModuleId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgShopSectionBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}
